#encoding: utf-8
class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :common_user?, :sys_admin?, :biz_unit_admin?, :current_user, :current_user_session

  private
  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.user
  end

  def require_login
    unless has_login? && has_right?(self)
      format = request.headers['HTTP_ACCEPT']
      if format.include?('application/json')
        render :json => {:status => 'exception', :notice => "您需要登录,请先登录"}
        return true
      end
      flash[:notice] = "您需要登录"
      redirect_to '/login'
    end
  end

  def has_login?
    current_user.nil? ? false : true
  end

  # 赋给用户访问权限
  def has_right?(obj)
    if current_user.admin?
      #[ShipownerMapsController,UsersController].each do |ctrl|
      return true # if obj.is_a? ctrl
      #end
    end
    if current_user.shipowner?
      [ShipownerMapsController, UsersController, UserSessionsController].each do |ctrl|
        return true if obj.is_a? ctrl
      end
    end
    if current_user.motorcade?
      [MotorcadeMapsController, UsersController, UserSessionsController].each do |ctrl|
        return true if obj.is_a? ctrl
      end
    end

    if current_user.cy?
      [CyMapsController].each do |ctrl|
        return true if obj.is_a? ctrl
      end
    end

    if current_user.forward?
      [ForwardMapsController].each do |ctrl|
        return true if obj.is_a? ctrl
      end
    end

    if current_user
      [UsersController, UserSessionsController].each do |ctrl|
        return true if obj.is_a? ctrl
      end
    end
    false
  end

  # 系统超级管理员
  def sys_admin?
    current_user.admin? if current_user
  end

  #企业单位管理员
  def biz_unit_admin?
    current_user.biz_unit_admin? if current_user
  end

  #普通用户
  def common_user?
    if has_login?
      if sys_admin? or biz_unit_admin?
        return false
      end
      return true
    end
    return false
  end


  #处理url中传入的page参数，防止分页显示异常
  #检测page异常，则置为1
  def clean_page
    max_page = session[:max_page] if  session[:max_page]
    if params[:page] and params[:page].strip != ""
      page = params[:page]
      #如果是整数                                                                      .
      if page.to_i.to_s == page
        page = page.to_i
        #超过最大页数
        if max_page < page
          params[:page] = max_page
        end
        return if page.to_i > 0
      end
    end
    params[:page] = 1
  end

  #初始化用户身份条件，用于查询属于本用户的数据
  def init_identity_condition(conditions)
    if current_user
      if current_user.shipper?
        conditions[:user_id] = current_user.id
      elsif current_user.forward?
        conditions[:forward_id] = current_user.biz_unit.id
      elsif current_user.cy?
        conditions[:cy_id] = current_user.biz_unit.id
      elsif current_user.motorcade?
        conditions[:motorcade_id] = current_user.biz_unit.id
      elsif current_user.shipowner?
        conditions[:shipowner_id] = current_user.biz_unit.id
      end
    end
    return conditions
  end

  before_filter :require_login
end
